package com.ruoyi.service.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.validation.Validator;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.service.mapper.ServiceChargeDiscountMapper;
import com.ruoyi.service.domain.ServiceChargeDiscount;
import com.ruoyi.service.service.IServiceChargeDiscountService;

/**
 * 折扣信息Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-04-21
 */
@Service
public class ServiceChargeDiscountServiceImpl implements IServiceChargeDiscountService 
{
    private static final Logger log = LoggerFactory.getLogger(ServiceChargeDiscountServiceImpl.class);

    @Autowired
    private ServiceChargeDiscountMapper serviceChargeDiscountMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询折扣信息
     * 
     * @param id 折扣信息主键
     * @return 折扣信息
     */
    @Override
    public ServiceChargeDiscount selectServiceChargeDiscountById(Long id)
    {
        return serviceChargeDiscountMapper.selectServiceChargeDiscountById(id);
    }

    /**
     * 查询折扣信息列表
     * 
     * @param serviceChargeDiscount 折扣信息
     * @return 折扣信息
     */
    @Override
    public List<ServiceChargeDiscount> selectServiceChargeDiscountList(ServiceChargeDiscount serviceChargeDiscount)
    {
        return serviceChargeDiscountMapper.selectServiceChargeDiscountList(serviceChargeDiscount);
    }

    /**
     * 新增折扣信息
     * 
     * @param serviceChargeDiscount 折扣信息
     * @return 结果
     */
    @Override
    public int insertServiceChargeDiscount(ServiceChargeDiscount serviceChargeDiscount)
    {
        serviceChargeDiscount.setCreateTime(DateUtils.getNowDate());
        return serviceChargeDiscountMapper.insertServiceChargeDiscount(serviceChargeDiscount);
    }

    /**
     * 修改折扣信息
     * 
     * @param serviceChargeDiscount 折扣信息
     * @return 结果
     */
    @Override
    public int updateServiceChargeDiscount(ServiceChargeDiscount serviceChargeDiscount)
    {
        serviceChargeDiscount.setUpdateTime(DateUtils.getNowDate());
        return serviceChargeDiscountMapper.updateServiceChargeDiscount(serviceChargeDiscount);
    }

    /**
     * 批量删除折扣信息
     * 
     * @param ids 需要删除的折扣信息主键
     * @return 结果
     */
    @Override
    public int deleteServiceChargeDiscountByIds(Long[] ids)
    {
        return serviceChargeDiscountMapper.deleteServiceChargeDiscountByIds(ids);
    }

    /**
     * 删除折扣信息信息
     * 
     * @param id 折扣信息主键
     * @return 结果
     */
    @Override
    public int deleteServiceChargeDiscountById(Long id)
    {
        return serviceChargeDiscountMapper.deleteServiceChargeDiscountById(id);
    }


    /**
   * 导入数据
   *
   * @param dataList 数据列表
   * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
   * @param operName 操作用户
   * @return 结果
   */
    @Override
    public String importData(List<ServiceChargeDiscount> dataList, Boolean isUpdateSupport, String operName){
        if (StringUtils.isNull(dataList) || dataList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ServiceChargeDiscount row : dataList)
        {
            try
            {

                BeanValidators.validateWithException(validator, row);
                insertServiceChargeDiscount(row);

                /*
                // 验证是否存在旧数据
                ServiceChargeDiscount old = serviceChargeDiscountMapper.selectServiceChargeDiscountByName(row.getName());
                if (StringUtils.isNull(old))
                {
                    row.setCreateBy(operName);
                    insertServiceChargeDiscount(row);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + row.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    row.setId(old.getId());
                    row.setUpdateBy(operName);
                    updateServiceChargeDiscount(row);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + row.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + row.getName() + " 已存在");
                }
                */
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum;// + "、数据 " + row.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
